/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package workforce.ui;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import org.apache.log4j.Logger;
import org.springframework.web.context.support.WebApplicationContextUtils;
import workforce.service.PageService;

/**
 *
 * @author TEComp
 */
//WebServlet(name = "UIServlet", urlPatterns = {"/gui"})
public class UIServlet extends HttpServlet {

    private Logger log = Logger.getLogger(this.getClass());

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String sid = request.getParameter("pageId");
        int id = (sid == null) ? 0 : Integer.parseInt(sid);
        log.info("Request page Id:[" + sid + "]");
        try {
            DataSource ds = this.getDataSource();
            Connection conn = ds.getConnection();
            PageService pageService = new PageService();
            pageService.setConnection(conn);
            UIPage page = pageService.findPageById(id);
            out.println(page.print());
            pageService.close();
        } catch (Exception ex) {
            log.fatal("Cann't run GUI.", ex);
        } finally {
            out.close();
        }
    }
    protected void inRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
    }
    
    protected void outRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }
    private DataSource getDataSource() {
        DataSource dataSource = null;
        try {
            if (dataSource == null) {
                dataSource = (DataSource) WebApplicationContextUtils.getWebApplicationContext(
                        this.getServletContext()).getBean("dataSource");
            }
            return dataSource;
        } catch (Exception ex) {
            log.fatal("Cann't open DataSource", ex);
        }
        return null;
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
